from flask_appbuilder import Model
from sqlalchemy import Column, ForeignKey, Integer, String, Text
from sqlalchemy.orm import relationship

from myapp.models.helpers import AuditMixinNullable
from myapp.models.model_model_version import Model_Version

metadata = Model.metadata


class ModelExport(Model, AuditMixinNullable):
    __tablename__ = 'model_export'
    id = Column(Integer, primary_key=True)

    # Defines a foreign key column that links to the 'id' in the 'model_version' table.
    model_version_id = Column(Integer, ForeignKey('model_version.id'))
    # Defines a many-to-one relationship to the Model_Version model.
    # This allows easy access to the model version object being exported (e.g., export_task.model_version).
    model_version = relationship(Model_Version, foreign_keys=[model_version_id])

    # Defines a column for the name of the export task.
    name = Column(String(100), nullable=False, comment='名称')
    # Defines a column for a user-provided description of the export.
    describe = Column(String(1000), nullable=True, default='', comment='描述')
    # Defines a column to store the status of the export process (e.g., 'pending', 'running', 'completed', 'failed').
    status = Column(String(100), nullable=True, comment='导出状态')
    # Defines a column to store any error messages if the export operation fails.
    err_msg = Column(Text(65536), nullable=True, default='', comment='错误信息')

    def __repr__(self):
        return self.id

    @property
    def export_name(self):
        return f'me-{self.model_version_id}'
